﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Configuration;
using System.Xml;
using System.Collections;

namespace InsideDL
{    

    public class MISFTP
    {
        protected string ftp = string.Empty;
        protected string id = string.Empty;
        protected string pw = string.Empty;
        protected WebResponse Response = null;
        protected FtpWebRequest FTP = null;
        public string ERRMSG = string.Empty;

        public MISFTP(string p_ftp, string p_id, string p_pw)
        {
            ftp = p_ftp;
            id = p_id;
            pw = p_pw;
        }

        protected void CreateFTP()
        {
            ERRMSG = string.Empty;
            if (FTP == null)
            {
                FTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp));                                
            }
            FTP.Credentials = new NetworkCredential(id,pw);
            FTP.KeepAlive = false;
            //reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            FTP.UseBinary = true;
            FTP.Proxy = null;
            FTP.UsePassive = false;
            FTP.UseBinary = true;
            //FTP.Credentials = new NetworkCredential(id, pw);
            //FTP.Method = WebRequestMethods.Ftp.ListDirectory;
            //FTP.Proxy = null;
            //FTP.KeepAlive = false;
            //FTP.UsePassive = false;
            //FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
            //Response = FTP.GetResponse() as FtpWebResponse;
        }

        /// <summary>
        /// 取回目前檔案內容
        /// </summary>
        /// <param name="p_uriFTP">FTP的位置EX: ftp://</param>
        /// <param name="p_id"></param>
        /// <param name="p_pw"></param>
        /// <returns></returns>
        public string[] GetFTPFileList()
        {
            CreateFTP();
            StringBuilder result = new StringBuilder();
            try
            {
                //FTP.UseBinary = true;
                //FTP.Credentials = new NetworkCredential(id, pw);
                FTP.Method = WebRequestMethods.Ftp.ListDirectory;
                //FTP.Proxy = null;
                //FTP.KeepAlive = false;
                //FTP.UsePassive = false;
                Response = FTP.GetResponse();
                using (StreamReader reader = new StreamReader(Response.GetResponseStream()))
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                }

            }
            catch (Exception ex)
            {
                ERRMSG = ex.Message;
                throw new Exception(ex.Message);
            }
            finally
            {
                if (Response != null)
                {
                    Response.Close();
                }            
            }
            // to remove the trailing '\n'               
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            return result.ToString().Split('\n');
        }

        public bool DownloadFile(string p_outPath, string[] p_fileName)
        {
            ERRMSG = string.Empty;
            bool ok = true;
            try
            {
                WebClient request = new WebClient();
                request.Credentials = new NetworkCredential(id, pw);
                foreach (string str in p_fileName)
                {
                    string dlFile = string.Format("{0}/{1}", ftp, str);
                    byte[] fileData = request.DownloadData(dlFile);
                    FileStream file = File.Create(
                                           string.Format(@"{0}\{1}", p_outPath, str));
                    file.Write(fileData, 0, fileData.Length);
                    file.Close();
                }

            }
            catch (Exception ex)
            {
                ERRMSG = ex.Message;
                ok = false;
            }
            return ok;
        }
    }
}
